import json

from bs4 import BeautifulSoup
from loguru import logger

from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.school.processor.base_processor import BaseProcessor
from com.arcfox.middleware import async_mysql_middleware as db


class SchoolMajorProcessor(BaseProcessor):

    def __init__(self):
        super().__init__()

    async def store_school_major(self, school_major_list, data_version):
        mapping = db.MysqlMapping("tb_university_major")
        for school_major_data in school_major_list:
            school_major_data["data_version"] = data_version
            university_uid = self.generate_school_uid(school_major_data["university_name"])

            school_major_data["university_uid"] = university_uid
            major_code = school_major_data["major_code"]
            major_name = school_major_data["major_name"]
            university_department = school_major_data["university_department"]

            # 不拦截可以入库，不需要统一删除
            if major_code == "":
                logger.info("majorCode 为空 {}", school_major_data)
            exist = await mapping.query(
                {
                    "major_code": major_code,
                    "university_uid": university_uid,
                    "major_name": major_name,
                    "university_department": university_department
                }, ["id"])
            if exist:
                logger.info("{} 已经存在，更新数据", major_code)
                await mapping.update(school_major_data, {"id": exist[0][0]})
                return
            await mapping.insert(school_major_data)
